package cn.taketoday.live.handler;

import com.alibaba.fastjson.JSONObject;

import cn.taketoday.live.WebSocketMessage;
import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.data.WelcomeVip;
import cn.taketoday.live.data.enums.ShieldMessage;
import cn.taketoday.live.utils.JodaTimeUtils;

/**
 * @author TODAY 2021/5/17 23:18
 */
public class WelcomeCommandHandler implements CommandHandler {

  @Override
  public void handle(CommandContext context) throws Exception {
    // 区分年月费老爷
    /*
     * if(welcomVip.getSvip()==1) {
     * System.out.println(JodaTimeUtils.getCurrentTimeString()+":欢迎年费老爷:"+welcomeVip
     * .getUname()+" 进入直播间"); }else {
     * System.out.println(JodaTimeUtils.getCurrentTimeString()+":欢迎月费老爷:"+welcomeVip
     * .getUname()+" 进入直播间"); }
     */

    final JSONObject jsonObject = context.getOriginal();
    final StringBuilder stringBuilder = new StringBuilder(200);
    if (context.contains(ShieldMessage.is_welcome)) {
      WelcomeVip welcomeVip = JSONObject.parseObject(jsonObject.getString("data"), WelcomeVip.class);
      stringBuilder.append(JodaTimeUtils.getCurrentDateTimeString());
      stringBuilder.append(":欢迎老爷:");
      stringBuilder.append(welcomeVip.getUname());
      stringBuilder.append(" 进入直播间");
      //控制台打印\

      if (context.contains(ShieldMessage.is_cmd)) {
        System.out.println(stringBuilder);
      }
      context.sendMessage(WebSocketMessage.of("welcomeVip", welcomeVip));
      if (GlobalData.logThread != null && !GlobalData.logThread.FLAG) {
        GlobalData.logString.add(stringBuilder.toString());
        synchronized(GlobalData.logThread) {
          GlobalData.logThread.notify();
        }
      }
      stringBuilder.delete(0, stringBuilder.length());
    }

    // LOGGER.debug("让我看看哪个老爷大户进来了:::" + message);
  }

}
